package com.thinksns.sociax.t4.service;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.util.Pair;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.thinksns.sociax.api.Api;
import com.thinksns.sociax.api.ApiCheckin;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.data.StaticInApp;
import com.thinksns.sociax.t4.exception.VerifyErrorException;
import com.thinksns.sociax.t4.model.ModelNotification;
import com.thinksns.sociax.thinksnsbase.exception.ApiException;
import com.thinksns.sociax.thinksnsbase.exception.DataInvalidException;
import com.thinksns.sociax.unit.AppIconBadgeManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 类说明：
 *
 * @author wz
 * @version 1.0
 * @date 2014-11-26
 */
public class UnReadMessageAndrLocationService extends Service implements AMapLocationListener {
    private static final String TAG = "UnReadMessageAndrLocationService";
    private static final int DEFAULT_UNREADMSG_SLEEP_TIME = 6000; // 获取未读消息的时间间隔
    private static final int UPDATE_TIME = 30 * 60 * 1000; // 半个小时定位一次并上传服务器

    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private NotificationManager notificationManager;
    private ModelNotification mdNotifyCation;
    private Handler handler;
    private ExecutorService threadPool;
    private boolean isExit = false;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        threadPool = Executors.newSingleThreadExecutor();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                threadPool.submit(new MyThread());
            }
        };
        // 初始化定位
        initLocation();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Message Server start ....");
        threadPool.submit(new CheckInThread());
        handler.sendEmptyMessage(1);
        // 启动定位
        locationClient.startLocation();
        return super.onStartCommand(intent, flags, startId);
    }


    //获取未读消息线程
    private class MyThread implements Runnable {
        @Override
        public void run() {
            if (!isExit)
                getUnreadCount();
        }
    }

    //获取签到线程
    private class CheckInThread implements Runnable {

        @Override
        public void run() {
            try {
                Object result = new Api.CheckinApi().getCheckInfo();
                Thinksns.setCheckIn(result);
                JSONObject jsonData = new JSONObject(result.toString());
                if (jsonData.getBoolean("ischeck")) {
                    //已经签到
                    getRankTask();
                }
            } catch (ApiException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    //获取签到排行榜
    private void getRankTask() {
        ApiCheckin apiCheckin = new Api.CheckinApi();
        try {
            Object object = apiCheckin.getCheckRankList();
            Thinksns.setRankInfo(object);
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean stopService(Intent name) {
        Log.i("mythread", "合理的关掉了这个线程哈");
        return super.stopService(name);
    }

    /***
     * 获取未读消息
     */
    public void getUnreadCount() {
        Thinksns app = (Thinksns) this.getApplicationContext();
        mdNotifyCation = new ModelNotification();
        if (app.getMessages() != null)
            try {
                mdNotifyCation = (ModelNotification) app.getMessages()
                        .getUnreadCount();
            } catch (VerifyErrorException e) {
                e.printStackTrace();
            } catch (DataInvalidException e) {
                e.printStackTrace();
            } catch (ApiException e) {
                e.printStackTrace();
            }
        // 发送广播
        if (mdNotifyCation.checkValid()) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("content", mdNotifyCation);
            intent.putExtras(bundle);
            intent.setAction(StaticInApp.SERVICE_NEW_NOTIFICATION);
            sendBroadcast(intent);
            int count = mdNotifyCation.getAtme() + mdNotifyCation.getComment() + mdNotifyCation.getDigg() + mdNotifyCation.getFollower() + mdNotifyCation.getWeibaComment();
            AppIconBadgeManager.setBadge(this, count);
        } else {
            Log.v("UnReadMessageAndrLocationService", "mdMotification is unv alid");
        }

        handler.sendEmptyMessageDelayed(1, DEFAULT_UNREADMSG_SLEEP_TIME);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != locationClient) {
            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
        isExit = true;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                Pair<String, String> location = Pair.create(
                        String.valueOf(aMapLocation.getLatitude()),
                        String.valueOf(aMapLocation.getLongitude()));
                sendNowLocation(location);
            }
        }

    }

    /**
     * 初始化位置
     */
    public void initLocation() {
        locationClient = new AMapLocationClient(getApplicationContext());
        locationOption = new AMapLocationClientOption();
        // 设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        locationOption.setInterval(UPDATE_TIME);
        // 设置定位监听
        locationClient.setLocationListener(this);
        // 设置定位参数
        locationClient.setLocationOption(locationOption);
    }

    /**
     * 发送当前位置
     */
    public void sendNowLocation(final Pair<String, String> location) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Thinksns app = (Thinksns) getApplicationContext();
                try {
                    app.getFindPeopleApi().updateLocation(location.first, location.second);
                } catch (ApiException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
